package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysOrderLogMapper;
import com.settlement.system.model.entity.SysOrderLog;
import com.settlement.system.model.form.SysOrderLogForm;
import com.settlement.system.model.query.SysOrderLogQuery;
import com.settlement.system.model.vo.SysOrderLogImportVo;
import com.settlement.system.service.SysOrderLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 刷单日志表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-18
 */
@Service
public class SysOrderLogServiceImpl extends ServiceImpl<SysOrderLogMapper, SysOrderLog> implements SysOrderLogService {

    @Override
    public Page<SysOrderLog> getPage(SysOrderLogQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysOrderLog> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysOrderLog(SysOrderLogForm sysOrderLogForm) {
        SysOrderLog sysOrderLog = new SysOrderLog();
        BeanUtils.copyProperties(sysOrderLogForm, sysOrderLog);
        sysOrderLog.setCreateTime(null);
        return this.saveOrUpdate(sysOrderLog);
    }

    @Override
    public boolean saveSysOrderLog(String email, String content) {
        SysOrderLog sysOrderLog = new SysOrderLog();
        sysOrderLog.setEmail(email);
        sysOrderLog.setContent(content);
        return this.saveOrUpdate(sysOrderLog);
    }

    @Override
    public SysOrderLogForm getSysOrderLogFormData(Long id) {
        SysOrderLog sysOrderLog = this.getById(id);
        SysOrderLogForm sysOrderLogForm = new SysOrderLogForm();
        BeanUtils.copyProperties(sysOrderLog, sysOrderLogForm);
        return sysOrderLogForm;
    }

    @Override
    public boolean deleteSysOrderLog(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysOrderLogImportVo> voList) {
        List<SysOrderLog> sysOrderLogList = new ArrayList<>();
        for (SysOrderLogImportVo importVo : voList) {
            SysOrderLog sysOrderLog = new SysOrderLog();
            BeanUtils.copyProperties(importVo, sysOrderLog);
            sysOrderLogList.add(sysOrderLog);
        }
        this.saveOrUpdateBatch(sysOrderLogList);
    }

    @Override
    public List<SysOrderLogImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysOrderLog> sysOrderLogList = this.getBaseMapper().selectBatchIds(ids);
        List<SysOrderLogImportVo> voList = new ArrayList<>();
        for (SysOrderLog sysOrderLog : sysOrderLogList) {
            SysOrderLogImportVo vo = new SysOrderLogImportVo();
            BeanUtils.copyProperties(sysOrderLog, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<SysOrderLog> logList(String email) {
        return this.list(new LambdaQueryWrapper<SysOrderLog>().eq(SysOrderLog::getEmail, email).orderByDesc(SysOrderLog::getCreateTime));
    }
}
